package com.alisls.shequtao.service.goods.service.impl;

import com.alisls.shequtao.common.model.result.PageResult;
import com.alisls.shequtao.service.goods.dao.SpuDAO;
import com.alisls.shequtao.service.goods.dto.BrandDTO;
import com.alisls.shequtao.service.goods.dto.CategoryDTO;
import com.alisls.shequtao.service.goods.dto.SpuDTO;
import com.alisls.shequtao.service.goods.dto.SpuDetailDTO;
import com.alisls.shequtao.service.goods.entity.SpuDO;
import com.alisls.shequtao.service.goods.entity.SpuDetailDO;
import com.alisls.shequtao.service.goods.query.SpuPageQuery;
import com.alisls.shequtao.service.goods.service.SpuDetailService;
import com.alisls.shequtao.service.goods.service.SpuService;
import com.alisls.shequtao.service.goods.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * SpuService
 *
 * @author Ke Wang
 */
@Service("spuService")
@AllArgsConstructor
public class SpuServiceImpl extends ServiceImpl<SpuDAO, SpuDO> implements SpuService {

    private final SpuDAO spuDAO;

    private final SpuDetailService spuDetailService;

    @Override
    public SpuDTO getSpuById(Long id) {
        SpuDO spuDO = spuDAO.selectById(id);
        if (spuDO == null) {
            return null;
        }
        return convertToDTO(spuDO);
    }

    @Override
    public PageResult<SpuDTO> getSpuByPage(SpuPageQuery spuPageQuery) {
        QueryWrapper<SpuDO> queryWrapper = new QueryWrapper<>();
        Integer saleable = spuPageQuery.getSaleable();
        if (saleable != null) {
            queryWrapper.eq("saleable", spuPageQuery.getSaleable());
        }

        String key = spuPageQuery.getKey();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.like("spu_name", key)
                    .or()
                    .like("sub_title", key);
        }

        String orderField = spuPageQuery.getOrderField();
        String orderType = spuPageQuery.getOrderType();
        if (!StringUtils.isEmpty(orderField)) {
            if ("asc".equalsIgnoreCase(orderType)) {
                queryWrapper.orderByAsc(orderField);
            } else {
                queryWrapper.orderByDesc(orderField);
            }
        }

        Page<SpuDO> page = new Page<>(spuPageQuery.getCurrentPage(), spuPageQuery.getPageSize());

        IPage<SpuDO> spuDOPage = spuDAO.selectPage(page, queryWrapper);

        PageResult<SpuDTO> spuDTOResult = PageUtil.convertToPageResult(spuDOPage);
        List<SpuDO> spuDOs = spuDOPage.getRecords();
        if (!CollectionUtils.isEmpty(spuDOs)) {
            List<SpuDTO> spuDTOs = spuDOs.stream()
                    .map(this::convertToDTO)
                    .collect(Collectors.toList());
            spuDTOResult.setRows(spuDTOs);
        }

        return spuDTOResult;
    }

    @Override
    public SpuDTO saveSpuAndSpuDetail(SpuDTO spuDTO) {
        SpuDO spuDO = this.convertToDO(spuDTO);
        if (spuDAO.insert(spuDO) == 1) {
            spuDTO.setId(spuDO.getId());
        }

        SpuDetailDO spuDetailDO = spuDetailService.convertToDO((SpuDetailDTO) spuDTO);
        spuDetailService.save(spuDetailDO);

        return spuDTO;
    }

    /**
     * 将DO转换成DTO
     * @param spuDO 商品SpuDO
     * @return 商品SpuDTO
     */
    @Override
    public SpuDTO convertToDTO(SpuDO spuDO) {
        SpuDTO spuDTO = new SpuDTO();
        BeanUtils.copyProperties(spuDO, spuDTO);

        Long brandId = spuDO.getBrandId();
        BrandDTO brandDTO = new BrandDTO();
        brandDTO.setId(brandId);
        spuDTO.setBrandDTO(brandDTO);

        Long categoryId = spuDO.getCategoryId();
        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setId(categoryId);
        spuDTO.setCategoryDTO(categoryDTO);

        Long categoryId2 = spuDO.getCategoryId2();
        CategoryDTO categoryDTO2 = new CategoryDTO();
        categoryDTO2.setId(categoryId2);
        spuDTO.setCategoryDTO2(categoryDTO2);

        Long categoryId3 = spuDO.getCategoryId3();
        CategoryDTO categoryDTO3 = new CategoryDTO();
        categoryDTO3.setId(categoryId3);
        spuDTO.setCategoryDTO3(categoryDTO3);

        return spuDTO;
    }

    @Override
    public SpuDO convertToDO(SpuDTO spuDTO) {
        SpuDO spuDO = new SpuDO();
        BeanUtils.copyProperties(spuDTO, spuDO);

        CategoryDTO categoryDTO = spuDTO.getCategoryDTO();
        if (categoryDTO != null) {
            spuDO.setCategoryId(categoryDTO.getId());
        }
        CategoryDTO categoryDTO2 = spuDTO.getCategoryDTO2();
        if (categoryDTO2 != null) {
            spuDO.setCategoryId2(categoryDTO2.getId());
        }
        CategoryDTO categoryDTO3 = spuDTO.getCategoryDTO3();
        if (categoryDTO3 != null) {
            spuDO.setCategoryId3(categoryDTO3.getId());
        }
        BrandDTO brandDTO = spuDTO.getBrandDTO();
        if (brandDTO != null) {
            spuDO.setBrandId(brandDTO.getId());
        }
        return spuDO;
    }
}
